package cc.plumgo.j2me.utils;

import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;

public class StringUtils {
	public static int changeLine(String str, Font font, int linewd) {
		int wd = 0;
		char ch;
		for (int i = 0; i < str.length(); i++) {
			ch = str.charAt(i);
			if (ch == '\n') {
				return i + 1;
			}
			wd += font.charWidth(ch);
			if (wd > linewd) {
				return i;
			}
		}
		return 0;
	}
	public static void adjustDrawString(Graphics g, String text, int linewd, int x, int y, int yDis) {
		String subStr;
		int nPos;
		while (true) {
			nPos = changeLine(text, g.getFont(), linewd);
			if (nPos == 0) {
				g.drawString(text, x, y, 0);
				break;
			} else {
				if (text.charAt(nPos - 1) == '\n') {
					subStr = text.substring(0, nPos - 1);
				} else {
					subStr = text.substring(0, nPos);
				}
				g.drawString(subStr, x, y, 0);
				text = text.substring(nPos, text.length());
				y += yDis;
			}
		}
	}
}
